package com.jf.exam.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.jf.exam.mapper.ClassMapper;
import com.jf.exam.mapper.StudentMapper;
import com.jf.exam.pojo.data.ClassDO;
import com.jf.exam.pojo.data.StudentDO;
import com.jf.exam.pojo.vo.ClassVO;
import com.jf.exam.pojo.vo.StudentListVO;
import com.jf.exam.pojo.vo.StudentVO;
import com.jf.exam.service.StudentService;
import com.jf.exam.utils.PageBean;
import com.jf.exam.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * 学生Service
 * <br/>
 * Created by weidong on 2018/07/18
 */
@Service("studentService")
public class StudentServiceImpl implements StudentService {

    @Resource
    private StudentMapper studentMapper;

    @Resource
    private ClassMapper classMapper;


    @Override
    public Result addStudent(StudentVO studentVO) throws Exception {
        Integer count = studentMapper.addStudent(studentVO);
        if (count > 0) {
            return Result.getSuccess("添加成功");
        }
        return Result.getFailure("失败成功");
    }

    @Override
    public Result updateStudent(StudentVO studentVO) throws Exception {
        StudentVO studentVO1 = new StudentVO();
        studentVO1.setSno(studentVO.getSno());
        List<StudentDO> studentDO = studentMapper.listStudent(studentVO1);
        if (studentDO== null && studentDO.isEmpty()) {
            return Result.getSuccess("学生不存在，修改失败");
        }
        ClassDO classDO = classMapper.findDetailClass(new ClassVO(studentDO.get(0).getFkClass()));
        if (classDO == null) {
            return Result.getSuccess("班级不存在，修改失败");
        }
        int count = studentMapper.updateStudent(studentVO);
        if (count > 0) {
            return Result.getSuccess("添加成功");
        }
        return Result.getFailure("失败成功");
    }

    @Override
    public Result findDetailStudent(StudentVO studentVO) throws Exception {
        return null;
    }


    /**
     * 查询学生列表
     */
    @Override
    public PageBean<StudentListVO> listStudent(StudentVO query) throws Exception {
        //防止页码越界
        Integer count = studentMapper.countStudent(query);
        int max = (count + query.getPageSize() - 1) / query.getPageSize();
        if (query.getPageCode() > max) {
            query.setPageCode(max);
        }

        PageHelper.startPage(query.getPageCode(), query.getPageSize());
        List<StudentListVO> classLists = studentMapper.listAssociation(query);

        //插件提供的分页信息
        PageInfo<StudentListVO> info = new PageInfo<>(classLists);

        //根据插件提供的分页信息，构造我们自己的分页信息
        PageBean<StudentListVO> pageBean = new PageBean<StudentListVO>(classLists, info.getPageSize()
                , query.getPageCode(), (int) info.getTotal(), query.getSize());
        System.out.println(classLists);
        return pageBean;
    }

    @Override
    public Result listStudentPage(StudentVO studentVO) throws Exception {
        return null;
    }

    @Override
    public Result countStudent(StudentVO studentVO) throws Exception {
        return null;
    }

    /**
     * 删除学生
     */
    @Override
    public Result deleteStudent(StudentVO studentVO) throws Exception {
        int count = studentMapper.deleteStudent(studentVO);
        if (count > 0) {
            return Result.getSuccess("删除成功");
        }
        return Result.getFailure("删除成功");
    }


    /**
     * 判断学生是否存在
     */
    @Override
    public Result isExist(StudentVO student) {
        List<StudentDO> studentDOS = studentMapper.listStudent(student);
        if (studentDOS != null && !studentDOS.isEmpty()) {
            return Result.getFailure("学生存在");
        }
        return Result.getSuccess("学生不存在");
    }


    /**
     * 通过学生名称获取学生信息
     */
    @Override
    public StudentDO getStudentByName(String username) {
        List<StudentDO> students = studentMapper.listStudent(new StudentVO(username));
        if (students != null && !students.isEmpty()) {
            return students.get(0);
        }
        return null;
    }

}